#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int left, right, n;
    string path;
    vector<string> ret;
    vector<string> generateParenthesis(int _n)
    {
        n = _n;
        dfs();
        return ret;
    }
    void dfs()
    {
        if (right == n)
        {
            ret.push_back(path);
            return;
        }

        if (left < n)
        {
            path.push_back('(');
            left++;
            dfs();
            path.pop_back();
            left--;
        }

        if (left > right)
        {
            path.push_back(')');
            right++;
            dfs();
            path.pop_back();
            right--;
        }
        return;
    }
};